package org.wyf.EveryPoint.common.controller;

import org.wyf.EveryPoint.common.annotation.ControllerEndpoint;
import org.wyf.EveryPoint.common.controller.BaseController;
import org.wyf.EveryPoint.common.entity.FebsConstant;
import org.wyf.EveryPoint.common.entity.FebsResponse;
import org.wyf.EveryPoint.common.entity.QueryRequest;
import org.wyf.EveryPoint.common.utils.FebsUtil;
import org.wyf.EveryPoint.common.entity.DictCategory;
import org.wyf.EveryPoint.common.service.IDictCategoryService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import lombok.RequiredArgsConstructor;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 字典类别表 Controller
 *
 * @author wyf
 * @date 2021-04-21 15:46:07
 */
@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class DictCategoryController extends BaseController {

    private final IDictCategoryService dictCategoryService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "dictCategory")
    public String dictCategoryIndex(){
        return FebsUtil.view("dictCategory/dictCategory");
    }

    @GetMapping("dictCategory")
    @ResponseBody
    @RequiresPermissions("dictCategory:list")
    public FebsResponse getAllDictCategorys(DictCategory dictCategory) {
        return new FebsResponse().success().data(dictCategoryService.findDictCategorys(dictCategory));
    }

    @GetMapping("dictCategory/list")
    @ResponseBody
    @RequiresPermissions("dictCategory:list")
    public FebsResponse dictCategoryList(QueryRequest request, DictCategory dictCategory) {
        Map<String, Object> dataTable = getDataTable(this.dictCategoryService.findDictCategorys(request, dictCategory));
        return new FebsResponse().success().data(dataTable);
    }

    @ControllerEndpoint(operation = "新增DictCategory", exceptionMessage = "新增DictCategory失败")
    @PostMapping("dictCategory")
    @ResponseBody
    @RequiresPermissions("dictCategory:add")
    public FebsResponse addDictCategory(@Valid DictCategory dictCategory) {
        this.dictCategoryService.createDictCategory(dictCategory);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "删除DictCategory", exceptionMessage = "删除DictCategory失败")
    @GetMapping("dictCategory/delete")
    @ResponseBody
    @RequiresPermissions("dictCategory:delete")
    public FebsResponse deleteDictCategory(DictCategory dictCategory) {
        this.dictCategoryService.deleteDictCategory(dictCategory);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改DictCategory", exceptionMessage = "修改DictCategory失败")
    @PostMapping("dictCategory/update")
    @ResponseBody
    @RequiresPermissions("dictCategory:update")
    public FebsResponse updateDictCategory(DictCategory dictCategory) {
        this.dictCategoryService.updateDictCategory(dictCategory);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改DictCategory", exceptionMessage = "导出Excel失败")
    @PostMapping("dictCategory/excel")
    @ResponseBody
    @RequiresPermissions("dictCategory:export")
    public void export(QueryRequest queryRequest, DictCategory dictCategory, HttpServletResponse response) {
        List<DictCategory> dictCategorys = this.dictCategoryService.findDictCategorys(queryRequest, dictCategory).getRecords();
        ExcelKit.$Export(DictCategory.class, response).downXlsx(dictCategorys, false);
    }
}
